package com.hundsun.documentripper.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.pojo.Product;
import com.hundsun.documentripper.domain.query.ProductQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.ProductService;
import com.hundsun.documentripper.utils.StringUtil;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：产品控制层
 * @date ：Created in 2021/1/26
 */
@Api("产品")
@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("新建产品")
    @PostMapping("/")
    @ErrorCatcher(code = "2001")
    public BaseResponse<Void> insertProduct(@RequestBody Product product, HttpServletRequest request) {
        product.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        productService.insertProduct(product);
        return new BaseResponse<Void>();
    }

    @ApiOperation("更新产品信息")
    @PutMapping("/")
    @ErrorCatcher(code = "2003")
    public BaseResponse<Void> updateProduct(@RequestBody Product product, HttpServletRequest request) {
        product.setModifyUser((String) request.getSession().getAttribute("userUuid"));
        productService.updateProduct(product);
        return new BaseResponse<Void>();
    }

    @ApiOperation("根据唯一标识获取产品对象")
    @GetMapping("/{uuid}")
    @ErrorCatcher(code = "2004")
    public BaseResponse<Product> getProductById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<Product>(productService.getProductById(uuid));
    }

    @ApiOperation("删除产品")
    @DeleteMapping("/{uuid}")
    @ErrorCatcher(code = "2005")
    @Transactional
    public BaseResponse<Void> deleteProduct(@PathVariable("uuid") String uuid) {
        productService.deleteProduct(uuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("分页获取用户的产品列表")
    @PostMapping("/{userUuid}")
    @ErrorCatcher(code = "2006")
    public BaseResponse<IPage<Product>> getProductListByPage(@RequestBody ProductQuery productQuery, @PathVariable("userUuid") String userUuid) {
        if (StringUtil.isEmpty(productQuery.getUserUuid())) {
            productQuery.setUserUuid(userUuid);
        }
        return new BaseResponse<IPage<Product>>(productService.getProductListByPage(productQuery));
    }

    @ApiOperation("用户新增产品权限")
    @PostMapping("/{uuid}/user/{userUuid}")
    @ErrorCatcher(code = "2007")
    public BaseResponse<Void> userBindProduct(@PathVariable("uuid") String uuid, @PathVariable("userUuid") String userUuid) {
        productService.userBindProduct(uuid, userUuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("用户移除产品权限")
    @DeleteMapping("/{uuid}/user/{userUuid}")
    @ErrorCatcher(code = "2008")
    public BaseResponse<Void> userUnbindProduct(@PathVariable("uuid") String uuid, @PathVariable("userUuid") String userUuid) {
        productService.userUnbindProduct(uuid, userUuid);
        return new BaseResponse<Void>();
    }

}